<?
    function getSystems($xml_file) {
        global $sistemas;
        //$objeto  = domxml_open_file($xml_file);
        //$raiz = $objeto->document_element();
        $objeto = new DOMDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("lin_systems");
        if ($filho->length > 0) {
            $temp = $filho->item(0)->getelementsbytagname("system");
            $j = 0;
            for($i = 0; $i < $temp->length; $i++) {
                $method = $temp->item($i)->getelementsbytagname("method");
                $conteudo = $method->item(0)->nodeValue;
                if ($conteudo == 'iterative') {
                $sistemas[$j] = $temp->item($i);
                $j++;
                }
            }
            return $sistemas;
        }
        else return null;
    }

    function getDim($indice) {
        global $sistemas, $dim;
        $dimensao = $sistemas[$indice]->getelementsbytagname("dim");
        $dim = $dimensao->item(0)->nodeValue;
        return $dim;
    }
    
    function getIter($indice) {
        global $sistemas, $k;
        $iteracoes = $sistemas[$indice]->getelementsbytagname("maxiter");
        $k = $iteracoes->item(0)->nodeValue;
        return $k;
    }

    function getEps($indice) {
        global $sistemas, $t;
        $precisao = $sistemas[$indice]->getelementsbytagname("epsilon");
        $t = $precisao->item(0)->nodeValue;
        return $t;
    }

    function getDesc($indice) {
        global $sistemas, $desc;
        $descricao = $sistemas[$indice]->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $sistemas, $com;
        $comentario = $sistemas[$indice]->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $sistemas, $ref;
        $referencia = $sistemas[$indice]->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function getMatrix($indice) {
        global $sistemas, $A;
        $linhas = $sistemas[$indice]->getelementsbytagname("row");
        for($i = 0; $i < $linhas->length; $i++) {
            $x = $linhas->item($i)->getelementsbytagname("x");
            for($j = 0; $j < ($x->length-1); $j++) {
                $A[$i][$j] = $x->item($j)->nodeValue;
            }
        }
        return $A;
    }
    
    function getVectorT($indice) {
        global $sistemas, $T;
        $linhas = $sistemas[$indice]->getelementsbytagname("row");
        for($j = 0; $j < $linhas->length; $j++) {
            $x = $linhas->item($j)->getelementsbytagname("x");
            $T[$j] = $x->item($x->length-1)->nodeValue;
        }
        return $T;
    }

    function getVectorX($indice) {
        global $sistemas, $vtx;
        $vetor = $sistemas[$indice]->getelementsbytagname("initial_vector");
        $x = $vetor->item(0)->getelementsbytagname("x");
        for($j = 0; $j < $x->length; $j++) {
            $vtx[$j] = $x->item($j)->nodeValue;
        }
        return $vtx;
    }

    function createMenu() {
        global $sistemas,$msg;
        for($i = 0; $i < sizeof($sistemas); $i++) {
            echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
            echo $msg->get('XML_EXIBE_SELA_MSG01');
            echo getDim($i);
            echo "</u></a><p>";
        }
        return;
    }
            
?>

<? if (empty($xml_file) && empty($id_bib)) $id_bib = "bib_demo.xml"; 
 if ( (strlen(@$indice)==0) || ($indice == null) ) { ?>
    <form name="myform" enctype="multipart/form-data" method="post">

    <script language="javascript" type="text/javascript">
        function createLinks(value) {
            document.myform.indice.value = value;
            document.myform.action = 'index.php?inc=jacobi.php&modo=exemplo';
            document.myform.submit();
        }

    </script>
    <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
    <? verificaBib($xml_file); ?>
    <?  if (file_exists($xml_file)) {
            $temp = getSystems($xml_file);
            if (!$temp==null) {
                echo "<p>";
                echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                echo "<p>";
                createMenu();
            } else {
                    echo $msg->get('XML_EXIBE_MSG05');
                    echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
            }
    } else {
                echo $msg->get('XML_EXIBE_MSG06');
                echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
    }
    ?>

    <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
    <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
    <input type="hidden" name="indice">
    <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG07'); ?> onClick="history.back()">
    </form>

<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {
            document.myform.Calcular.value = 'sim';
            document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=jacobi.php&modo=exemplo';
            document.myform.submit();
            }

        </script>

        <? getSystems($xml_file); ?>
        <? $dim = getDim($indice); ?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>
        <? $A = getMatrix($indice); ?>
        <? $T = getVectorT($indice); ?>
        <? $vtx = getVectorX($indice); ?>
        <? $t = getEps($indice); ?>
        <? $k = getIter($indice); ?>

        <? verificaBib($xml_file); ?>
        <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_SELA_MSG02'); ?> </b><? echo $dim ?></font>
        <input type="hidden" name=dim value="<? echo $dim ?>">
        <p>
        <? if (!strlen($desc) == 0) { ?>
            <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_SELA_MSG03'); ?> </b><? echo utf8_decode($desc) ?></font>
        <? } ?>
        <p>
        <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_SELA_MSG05'); ?></b></font>
        <input type="text" size='2' maxlength='4' name="k" value="<? echo $k ?>">
        <p>
        <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_SELA_MSG06'); ?> 10</b><sup>-
        <? echo $t ?>
        </sup></font>
        <input type="hidden" name="t" value="<? echo $t ?>">
        <p>
        <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_SELA_MSG04'); ?></b></font>
        <p>
        <table border="0">
        <? for ($i=0; $i<$dim; $i++) {	?>
            <tr>
        <?	for ($j=0; $j<$dim; $j++) {
                $aux=$j+1;  ?>
                <td align=right valign=top> <? echo $A[$i][$j] ?> </td>
                <input type="hidden" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo $A[$i][$j] ?>">
                <td align=right><font face="Arial">x<sub><? echo ("$aux") ?></sub></font></td>
                <?  if ($j<($dim-1)) { ?>
                    <td align=center width="20"> <? echo (" + "); ?> </td>
                <? }
            } /* end for j */?>
            <td align=center width="20"> <? echo (" = ");  ?></td>
            <td align=right valign=top><? echo $T[$i] ?></td>
            <input type="hidden" name="T[<? echo $i ?>]" value="<? echo $T[$i] ?>">
            </tr>
        <? } //end for i ?>
        </table>
        <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_SELA_MSG07'); ?></b><sub><font size="2"><b>i</b></sub> :</font>
        <br><br>
        <? for ($i=0; $i<$dim; $i++) {
            $aux=$i+1; ?>
            <input type="text" name="vtx[<? echo $i ?>]" value="<? echo $vtx[$i] ?>" maxlenght="10" size="5">
            <font face="Arial">X<sub><? echo ("$aux") ?></sub></font>
            <? if ($i<($dim-1)) echo ("  ;  "); ?>
        <? } ?>
        <p>
        <? if (!$com == null) { ?>
            <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG08');?></b><? echo utf8_decode($com) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
            <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG09'); ?> </b><? echo utf8_decode($ref) ?></font>
        <? } ?>
        <p>


        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="Calcular" >
    <input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG010'); ?> onClick="sendToCalculus()"><p>
        </form>
    
<?
    echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
    echo "<script language=\"javascript\" type=\"text/javascript\">\n";
    echo "function criarLinks(value) {\n";
    echo "document.exemplos.indice.value = value;\n";
    echo "document.exemplos.action = 'index.php?inc=jacobi.php&modo=exemplo'\n";
    echo "document.exemplos.submit();\n";
    echo "}\n";
    echo "</script>";
    echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
    echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
    echo "<input type=\"hidden\" name=\"indice\">";
    echo $msg->get('Outros_Exemplos')."<br>";

    $objeto = new DOMDocument();
    $objeto->load($xml_file);
    $filho = $objeto->getelementsbytagname("lin_systems");
    if ($filho->length > 0)
    {
        $temp = $filho->item(0)->getelementsbytagname("system");
        $j = 0;
        for($i = 0; $i < $temp->length; $i++)
        {
            $method = $temp->item($i)->getelementsbytagname("method");
            $conteudo = $method->item(0)->nodeValue;
            $dim = $temp->item($i)->getelementsbytagname("dim");
            $dim = $dim->item(0)->nodeValue;
            if ($conteudo == 'iterative')
            {
                if ($j != 0) echo " | ";
                if ($j == $indice) echo "<b>";
                echo "<a class=\"bibliotecaexemplos\" href=\"javascript:criarLinks($j)\" title=\"";
                echo "Dimens&atilde;o $dim\"><u>".($j+1)."</u></a>";
                if ($j == $indice) echo "</b>";
                $j++;
            }
        }
    }
    echo "<br>";
?>

<? } ?>
